﻿using System.Management.Automation;
using DotNet.DevOps.Tasks.Oracle;
using DotNet.DevOps.Tasks.Util;

namespace DotNet.DevOps.Tasks.Cmdlets
{
    /// <summary>
    /// Runs a sql script against an Oracle database.
    /// Can fail on script error if FailOnScriptError is set to true. 
    /// 
    /// Requirements:
    /// * Oracle driver with SQLPlus, must be installed.
    /// 
    /// Default behaviour:
    /// * Fails if logon is not possible.
    /// * Do not fail if the script has errors.
    /// * No timeout.
    /// 
    /// </summary>
    /// <example>
    /// import-module .\DotNet.DevOps.Tasks.dll
    /// 
    /// Invoke-OracleScript -Property @{
    ///                                  Files="MyScript.sql;MySecondScript.sql" 
    ///                                  Database="TEST"
    ///                                  User="SYS" 
    ///                                  Password="SYSPASSWORD"  
    ///                                  Timeout="10000"
    ///                                }
    /// </example>
    [Cmdlet(VerbsLifecycle.Invoke, "OracleScript", ConfirmImpact = ConfirmImpact.High, SupportsShouldProcess = false, SupportsTransactions = false)]
    public class Invoke_OracleScript : Cmdlet, IOracleScript
    {
        /// <summary>
        /// Script files to run separated by ;
        /// </summary>
        [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Files { get; set; }
        /// <summary>
        /// TNSNAME of the database to run the script against.
        /// </summary>
        [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Database { get; set; }
        /// <summary>
        /// User name to connect as. Ex. SAM, SYS.
        /// </summary>
        [Parameter(Mandatory = true, Position = 2, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string User { get; set; }
        /// <summary>
        /// Password.
        /// </summary>
        [Parameter(Mandatory = true, Position = 3, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Password { get; set; }
        /// <summary>
        /// Should the task fail when a logon error occours. Default true.
        /// </summary>
        [Parameter(Mandatory = false, Position = 4, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public bool FailOnLogonError { get; set; }
        /// <summary>
        /// Should the task fail when an error occours in a script. Default false.
        /// </summary>
        [Parameter(Mandatory = false, Position = 5, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public bool FailOnScriptError { get; set; }
        /// <summary>
        /// Timeout for script to run to completion in milliseconds. Negative values means unlimited. Default unlimited.
        /// </summary>        
        [Parameter(Mandatory = false, Position = 6, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public int Timeout { get; set; }

        private OracleScript task;

        public Invoke_OracleScript()
        {
            task = new OracleScript(new PowershellCmdletLogger(this));
            FailOnLogonError = task.FailOnLogonError;
            FailOnScriptError = task.FailOnScriptError;
            Timeout = task.Timeout;
        }

        protected override void BeginProcessing()
        {
            task = new OracleScript(new PowershellCmdletLogger(this))
                .CopyPropertyValuesFrom(this) as OracleScript;
        }

        protected override void ProcessRecord()
        {
            task.Execute();
        }
    }
}
